package com.jrx.anytxn.accounting.job.consolidationcheck.config;

import com.jrx.anytxn.accounting.bean.ConsolidationCheckBean;
import com.jrx.anytxn.accounting.config.AnytxnBatchProperties;
import com.jrx.anytxn.accounting.entity.GaVoucherInfo;
import com.jrx.anytxn.accounting.job.consolidationcheck.step.ConsolidationCheckListener;
import com.jrx.anytxn.accounting.job.consolidationcheck.step.ConsolidationCheckProcessor;
import com.jrx.anytxn.accounting.job.consolidationcheck.step.ConsolidationCheckWriter;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 并账检查任务
 *
 * @author qingyao
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class ConsolidationCheckConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;
    @Autowired
    private IOrganizationTableService organizationTableService;

    @Autowired
    private ConsolidationCheckProcessor consolidationCheckProcessor;

    @Autowired
    private ConsolidationCheckWriter consolidationCheckWriter;
    @Autowired
    private PlatformTransactionManager platformTransactionManager;
    @Autowired
    private ConsolidationCheckListener consolidationCheckListener;

    @Bean
    public Job consolidationCheckJob() {
        return jobBuilderFactory.get("consolidationCheckJob")
                .start(consolidationCheckStep())
                .listener(consolidationCheckListener)
                .build();
    }

    @Bean
    public Step consolidationCheckStep() {
        return stepBuilderFactory.get("consolidationCheckStep")
                .<GaVoucherInfo, ConsolidationCheckBean>chunk(anytxnBatchProperties.getConsolidationCheckConfiguration().getChunkLimit())//表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(consolidationCheckReader())
                .processor(consolidationCheckProcessor)
                .writer(consolidationCheckWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getConsolidationCheckConfiguration().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<GaVoucherInfo> consolidationCheckReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        Date businessDate = org.getToday();
        MyBatisCursorItemReader<GaVoucherInfo> consolidationCheckCursorReader = new MyBatisCursorItemReader<>();
        consolidationCheckCursorReader.setSaveState(false);
        consolidationCheckCursorReader.setSqlSessionFactory(sqlSessionFactory);
        consolidationCheckCursorReader.setQueryId("com.jrx.anytxn.accounting.mapper.ext.ExtVoucherInfoMapper.selectVoucherByDateForBatch");
        Map<String, Object> map = new HashMap<>();
        map.put("businessDate",businessDate);
        consolidationCheckCursorReader.setParameterValues(map);
        //同步reader   线程安全
        SynchronizedItemStreamReader<GaVoucherInfo> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(consolidationCheckCursorReader);
        return synchronizedItemStreamReader;
    }

}
